﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Modules.Orders.Services;
using Autorent.Desktop.Modules.Orders.Views;
using Autorent.Desktop.Modules.Orders.ViewModels;

namespace Autorent.Desktop.Modules.Orders
{
    /// <summary>
    /// The module contains work with orders. Includes create, update, delete and search operations.
    /// In addition includes an order confirmation and payment.
    /// </summary>
    [Module(ModuleName = "OrdersModule")]
    public class OrdersModule : IModule
    {
        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;

        public OrdersModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }

        public void Initialize()
        {
            _container.RegisterType(typeof(IAutoOrdersRepository), typeof(AutoOrdersRepository));
            _container.RegisterType(typeof(IPaymentsRepository), typeof(PaymentsRepository));
            _container.RegisterType(typeof(ISystemUsersRepository), typeof(SystemUsersRepository));

            if(User.IsRole("Administrator"))
            {
                _regionManager.AddToRegion("ListRegion", _container.Resolve<OrdersListViewModel>(new ParameterOverrides() { { "filterType", OrdersListViewModel.OrdersFilterType.All } }).View);
            }
            else if (User.IsRole("Manager"))
            {
                _regionManager.AddToRegion("ListRegion", _container.Resolve<OrdersListViewModel>(new ParameterOverrides() { { "filterType", OrdersListViewModel.OrdersFilterType.ByManager } }).View);
            }
            
        }
    }
}
